﻿using Common.Contract.Authentication.Record;
using Common.Contract.Paging;
using Common.Contract.ProductHandbook.Record;
using Common.Contract.ProductHandbook.Request;
using Common.Contract.ProductHandbook.Response;
using Common.Contract.SystemConfig.Record;
using Common.Service.Command;
using CrossSelling.Model.Interface;
using CrossSelling.Model.Model;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;

namespace CrossSelling.Core.ServiceCommand
{
    public class RetrieveChecklistCustomerByDaoServiceCommand : ProviderServiceCommand<IProviderFactory, RetrieveChecklistCustomerByDaoRequest, RetrieveChecklistCustomerByDaoResponse>
    {
        #region Properties
        public UserRecord Dao;
        public FilterRecord TextSearch;
        public int pageSize = 10, pageIndex = 0, totalRecord = 0;
        public int Product;
        #endregion
        public RetrieveChecklistCustomerByDaoServiceCommand(IProviderFactory providers) : base(providers) { }
        protected override void Validate(RetrieveChecklistCustomerByDaoRequest request)
        {
            if (request == null) throw new ArgumentNullException("request");
            if (!request.Header.ApplicationKey.HasValue || request.Header.ApplicationKey == Guid.Empty)
            {
                throw new ArgumentException("ApplicationKey must be set", "request");
            }
            if (string.IsNullOrEmpty(request.ProductId))
            {
                throw new ArgumentException("Product must be set", "request");
            }
            else
            {
                Int32.TryParse(request.ProductId, out Product);
            }

            if (request.CurrentUser == null)
            {
                throw new ArgumentException("MSBO Dao must be set", "request");
            }
            else
            {
                Dao = Request.CurrentUser;
            }
            TextSearch = Request.Filter;
            if (Request.Page != null)
            {
                pageIndex = Request.Page.PageIndex ?? 0;
                pageSize = Request.Page.PageSize ?? 0;
            }
        }

        protected override bool OnExecute(RetrieveChecklistCustomerByDaoResponse response)
        {
            bool isSuccess = true;
            string errorMessage = string.Empty;
            try
            {
                List<ChecklistCustomerRecord> records = new List<ChecklistCustomerRecord>();

                List<ChecklistCustomer> listCustomer = new List<ChecklistCustomer>();
                listCustomer = Providers.ProductHandbooks.RetrieveListCustomerChecklist(out totalRecord, Product, Dao, TextSearch, Request.Lang, pageSize, pageIndex);
                if (listCustomer != null)
                {
                    records = listCustomer.Select(x => new ChecklistCustomerRecord
                    {
                        Ccid = x.Ccid,
                        Branch = x.Branch,
                        CustName = x.CustName,
                        Dao = x.Dao,
                        DaoName = x.DaoName,
                        DateAdd = x.DateAdd,
                        DateUpdate = x.DateUpdate,
                        LegalId = x.LegalId,
                        ProductId = x.ProductId,
                        Region = x.Region,
                        Cif = x.Cif
                    }).ToList();
                }

                response.Records = records;
                response.Page = new PageRecord
                {
                    PageIndex = pageIndex,
                    PageSize = pageSize,
                    TotalRecords = totalRecord
                };
            }
            catch (Exception e)
            {
                isSuccess = false;
                errorMessage = e.Message;
            }
            response.ErrorMessage = errorMessage;
            return isSuccess;
        }
    }
}
